package health.lm.com.shop.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.androidkun.xtablayout.XTabLayout;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jakewharton.rxbinding2.view.RxView;
import com.vondear.rxtool.RxSPTool;
import com.vondear.rxtool.view.RxToast;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import health.lm.com.component_base.arouter.Router;
import health.lm.com.component_base.helper.ImageLoaderHelper;
import health.lm.com.component_base.widget.SuperDividerItemDecoration;
import health.lm.com.data.AppModel;
import health.lm.com.network.callback.SimpleCallBack;
import health.lm.com.router.ARouterConstant;
import health.lm.com.shop.R;
import health.lm.com.shop.activity.HLAllCompanyActivity;
import health.lm.com.shop.activity.HLPeopleActivity;
import health.lm.com.shop.entity.CompanyDynamicEntity;
import health.lm.com.shop.entity.HLAllCompanyEntity;
import health.lm.com.shop.entity.PeopleChainEntity;
import health.lm.com.shop.entity.PeopleChainMultiEntity;
import health.lm.com.shop.model.MallModel;
import health.lm.com.shop.widget.CircleMenuLayout;
import io.reactivex.functions.Consumer;

import static health.lm.com.router.ARouterConstant.FROM_TYPE_PEOPLE_LIST;

/**
 * Created by Android Studio .
 * Project：hlian health.lm.com.shop.adapter
 * Date：2019/8/14 17:17
 * Introduce：人脉连首页
 */
public class PeopleChainAdapter extends BaseMultiItemQuickAdapter<PeopleChainMultiEntity, BaseViewHolder> {
    private FragmentManager mFragmentManager;
    private Consumer<Object> type = v -> {
        //跳转全部商会
        ARouter.getInstance().build(ARouterConstant.HLAllCompanyActivity)
                .withInt("type", HLAllCompanyActivity.TYPE_BUSINESS)
                .navigation();
    };

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public PeopleChainAdapter(List<PeopleChainMultiEntity> data, FragmentManager pFragmentManager) {
        super(data);
        this.mFragmentManager = pFragmentManager;
        /*轮播*/
        addItemType(PeopleChainMultiEntity.TYPE_BANNER, R.layout.home_people_chain_banner);
        /*上部四个按钮*/
        addItemType(PeopleChainMultiEntity.TYPE_CATEGORY, R.layout.home_people_chain_button);
        /*上部商会推荐*/
        addItemType(PeopleChainMultiEntity.TYPE_RECOMMEND_COMPANY, R.layout.home_people_chain_recommand);
        /*我的商会*/
        addItemType(PeopleChainMultiEntity.TYPE_MY_COMPANY, R.layout.home_people_chain_my_company);
        /*发掘人脉*/
        addItemType(PeopleChainMultiEntity.TYPE_DISCOVER_PEOPLE, R.layout.home_people_chain_discover);
        /*商会动态*/
        addItemType(PeopleChainMultiEntity.TYPE_COMPANY_DYNAMIC, R.layout.home_people_chain_dynamic);
        /*底部商会推荐*/
        addItemType(PeopleChainMultiEntity.TYPE_RECOMMEND_COMPANY_2, R.layout.home_people_chain_recommand);
    }

    /**
     * Implement this method and use the helper to adapt the view to the given item.
     *
     * @param helper A fully initialized helper.
     * @param item   The item that needs to be displayed.
     */
    @Override
    protected void convert(@NonNull BaseViewHolder helper, PeopleChainMultiEntity item) {
        switch (helper.getItemViewType()) {
            case PeopleChainMultiEntity.TYPE_BANNER:
                Banner banner = helper.getView(R.id.banner);
                banner.setImages(item.getBanner());
                banner.setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object path, ImageView imageView) {
                        PeopleChainEntity.BannerEntity entity = (PeopleChainEntity.BannerEntity) path;
                        ImageLoaderHelper.getInstance().load(mContext, entity.getImage(), imageView);
                    }
                });
                banner.setOnBannerListener(position -> {
                    PeopleChainEntity.BannerEntity entity = item.getBanner().get(position);
                    if (TextUtils.isEmpty(entity.getUrl())) return;
                    ARouter.getInstance().build(ARouterConstant.JSWebActivity).withString("url", entity.getUrl()).navigation();
                });
                banner.start();
                break;
            case PeopleChainMultiEntity.TYPE_CATEGORY:
                //我的商会
                RxView.clicks(helper.getView(R.id.textView57)).throttleFirst(1, TimeUnit.SECONDS).subscribe(v -> {
                    ARouter.getInstance().build(ARouterConstant.HLMyCompanyActivity)
                            .withString("id", RxSPTool.getString(mContext, AppModel.my_business_id))
                            .navigation();
                });
                //我的人脉
                RxView.clicks(helper.getView(R.id.textView58)).throttleFirst(1, TimeUnit.SECONDS).subscribe(v -> {
                    ARouter.getInstance().build(ARouterConstant.HLPeopleActivity)
                            .withInt("from_type", FROM_TYPE_PEOPLE_LIST).navigation();
                });
                //全部商会
                RxView.clicks(helper.getView(R.id.textView59)).throttleFirst(1, TimeUnit.SECONDS).subscribe(type);
                //影响力中心
                RxView.clicks(helper.getView(R.id.textView60)).throttleFirst(1, TimeUnit.SECONDS).subscribe(v -> {
                    ARouter.getInstance().build(ARouterConstant.HLAllCompanyActivity)
                            .withInt("type", HLAllCompanyActivity.TYPE_INFLUENCE)
                            .navigation();
                });
                break;
            case PeopleChainMultiEntity.TYPE_RECOMMEND_COMPANY://上部分商会推荐
                helper.setText(R.id.textView2, "商会推荐").setText(R.id.textView3, "查看更多");
                RxView.clicks(helper.getView(R.id.textView3)).throttleFirst(1, TimeUnit.SECONDS).subscribe(type);
                RecyclerView rv_recommend = helper.getView(R.id.rv_list);
                rv_recommend.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
                PeopleChainRecommendAdapter recommendAdapter = new PeopleChainRecommendAdapter(item.getRecommend());
                rv_recommend.setAdapter(recommendAdapter);
                recommendAdapter.setOnItemClickListener((adapter, view, position) -> {
                    PeopleChainEntity.RecommendEntity entity = (PeopleChainEntity.RecommendEntity) adapter.getData().get(position);
                    ARouter.getInstance().build(ARouterConstant.HLMyCompanyActivity)
                            .withString("id", entity.getId())
                            .withString("title", "商会详情")
                            .navigation();
                });
                break;
            case PeopleChainMultiEntity.TYPE_MY_COMPANY:
                ImageLoaderHelper.getInstance().load(mContext, item.getMy_business().getImage(), helper.getView(R.id.imageView17));
                helper.setText(R.id.textView4, "我的商会")
                        .setText(R.id.textView61, item.getMy_business().getName())
                        .setText(R.id.textView62, "商会领导")
                        .setText(R.id.textView63, "组织架构")
                        .setText(R.id.textView64, "商会会员(" + item.getMy_business().getMember() + ")");
                //商会领导
                RxView.clicks(helper.getView(R.id.textView62)).throttleFirst(1, TimeUnit.SECONDS).subscribe(v -> {
                    ARouter.getInstance().build(ARouterConstant.HLCompanyLeaderActivity)
                            .withString("id", RxSPTool.getString(mContext, AppModel.my_business_id))
                            .navigation();
                });
                //组织架构
                RxView.clicks(helper.getView(R.id.textView63)).throttleFirst(1, TimeUnit.SECONDS).subscribe(v -> {
                    ARouter.getInstance().build(ARouterConstant.OrganizationChartActivity)
                            .withString("id", RxSPTool.getString(mContext, AppModel.my_business_id))
                            .navigation();
                });
                //商会会员
                RxView.clicks(helper.getView(R.id.textView64)).throttleFirst(1, TimeUnit.SECONDS).subscribe(v -> {
                    ARouter.getInstance().build(ARouterConstant.CompanyMemberListActivity)
                            .withString("id", RxSPTool.getString(mContext, AppModel.my_business_id))
                            .navigation();
                });
                break;
            case PeopleChainMultiEntity.TYPE_DISCOVER_PEOPLE:
                helper.setText(R.id.textView4, "发掘人脉");
                List<String> ids = new ArrayList<>();
                List<String> images = new ArrayList<>();
                List<String> names = new ArrayList<>();
                List<String> tags = new ArrayList<>();
                for (PeopleChainEntity.DiscoverEntity entity : item.getDiscover()) {
                    ids.add(entity.getId());
                    images.add(entity.getAvatar());
                    names.add(entity.getPosition());
                    tags.add(entity.getBusiness());
                }
                CircleMenuLayout menuLayout = helper.getView(R.id.id_menulayout);
                menuLayout.setMenuItemIconsAndTexts(images, names, tags);
                menuLayout.autoPlay();
                menuLayout.setOnMenuItemClickListener(new CircleMenuLayout.OnMenuItemClickListener() {
                    @Override
                    public void itemClick(View view, int pos) {
                        PeopleChainEntity.DiscoverEntity entity = item.getDiscover().get(pos);
                        ARouter.getInstance().build(ARouterConstant.HLPeopleDetailActivity)
                                .withString("id", entity.getId())
                                .withString("name", entity.getReal_name())
                                .navigation();
                    }

                    @Override
                    public void itemCenterClick(View view) {
                        ARouter.getInstance().build(ARouterConstant.PeopleDiscoverActivity).navigation();
                        menuLayout.autoPlay();
                        //RxToast.normal("中间按钮");
                    }
                });
                RxView.clicks(helper.getView(R.id.textView67)).throttleFirst(2, TimeUnit.SECONDS).subscribe(v -> {
                    MallModel.getInstance().attention(ids, 1, new SimpleCallBack<Object>() {
                        @Override
                        public void onSuccess(Object pO) {
                        }
                    });
                });
                break;
            case PeopleChainMultiEntity.TYPE_COMPANY_DYNAMIC:
                helper.setText(R.id.textView2, "商会动态").setText(R.id.textView3, "查看更多");
                RxView.clicks(helper.getView(R.id.textView3)).throttleFirst(1, TimeUnit.SECONDS).subscribe(v -> {
                    //跳转商会动态
                    ARouter.getInstance().build(ARouterConstant.HLCompanyDynamicActivity).navigation();
                });
                ViewPager viewPager = helper.getView(R.id.viewPager);
                XTabLayout xTabLayout = helper.getView(R.id.xTablayout);
                xTabLayout.setxTabDisplayNum(5);

                List<CompanyDynamicEntity> entities = item.getDynamic();
                PeopleChainPageAdapter adapter = new PeopleChainPageAdapter(mFragmentManager, entities);
                viewPager.setAdapter(adapter);
                viewPager.setOffscreenPageLimit(3);
                //将TabLayout和ViewPager关联起来。
                xTabLayout.setupWithViewPager(viewPager);
                break;
            case PeopleChainMultiEntity.TYPE_RECOMMEND_COMPANY_2://下部分商会推荐
                helper.setText(R.id.textView2, "商会推荐").setText(R.id.textView3, "查看更多");
                RxView.clicks(helper.getView(R.id.textView3)).throttleFirst(1, TimeUnit.SECONDS).subscribe(type);
                RecyclerView recyclerView = helper.getView(R.id.rv_list);
                HLAllCompanyAdapter companyAdapter = new HLAllCompanyAdapter(item.getBusiness_recommend());
                recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                recyclerView.addItemDecoration(new SuperDividerItemDecoration(new SuperDividerItemDecoration.Builder(mContext)));
                recyclerView.setAdapter(companyAdapter);
                companyAdapter.setOnItemClickListener((adapter1, view, position) -> {
                    HLAllCompanyEntity entity = (HLAllCompanyEntity) adapter1.getData().get(position);
                    ARouter.getInstance().build(ARouterConstant.HLMyCompanyActivity)
                            .withString("id", entity.getId())
                            .withString("title", "商会详情")
                            .navigation();
                });
                companyAdapter.setOnItemChildClickListener((adapter1, view, position) ->{
                    HLAllCompanyEntity entity = (HLAllCompanyEntity) adapter1.getData().get(position);
                    if (view.getId() == R.id.textView47) {
                        if (entity.getAttention().equals("1")) return;
                        List<String> follow_id = new ArrayList<>();
                        follow_id.add(entity.getId());
                        MallModel.getInstance().attention(follow_id, 2, new SimpleCallBack<Object>() {
                            @Override
                            public void onSuccess(Object pO) {
                                String attention = entity.getAttention().equals("1") ? "0" : "1";
                                entity.setAttention(attention);
                                adapter1.notifyDataSetChanged();
                            }
                        });
                    }
                });
                break;
        }
    }
}
